package com.financing.wap.service.impl;

import com.financing.sms.service.MessageService;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.SwitchDTO;
import com.financing.wap.service.ISmsNewService;
import com.financing.wap.service.ISystemService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class SmsNewServiceImpl implements ISmsNewService {

	private static final Logger log = LoggerFactory.getLogger(SmsNewServiceImpl.class);

	/**
	 * 发送开关
	 */
	private static String SWITCH_TURN_ON = "1";

	@Autowired
	public ISystemService systemService;

	@Autowired
	public MessageService messageServiceImpl;

	@Override
	public ResultMap sendSms(List<String> mobileList, String message,String smsType){

		try {

			return doSend(mobileList, message,smsType);
		} catch (Exception e) {

			log.error("error when send sms ^^", e);

			ResultMap result = new ResultMap();
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.VERIFYCODE_SEND_ERROR);
			result.setMessage("发送短信息异常！");
			return result;
		}
	}
	@Override
	public ResultMap sendOtpMessage(String mobile,String message,String smsType){

		ResultMap result = new ResultMap();

		try {
			List<String> mobileList = new ArrayList<String>();
			mobileList.add(mobile);
			//String randomStr = RandomUtils.getRandomUtils(6);
			result = doSend(mobileList, message,smsType);
			log.debug(message);
		} catch (Exception e) {
			log.error("error when send otp message~~", e);
			result.setStatus(ResultMap.STATUS.FAILE);
			result.setMessage("发送OTP消息失败");
		}

		return result;
	}

	@Override
	public boolean smsSwitch(String smsType){

		try{
			SwitchDTO switchDTO = systemService.getSwitch(smsType);
			if(switchDTO != null && SWITCH_TURN_ON.equals(switchDTO.getValue())){
				log.info("sms switch is on!");
				return true;
			}
		}catch(Exception e){
			log.error("error when query sms switch", e);
			return false;
		}
		log.info("sms switch is off!");
		return false;

	}

	/**
	 * 执行发送
	 * @param mobileList
	 * @param message
	 * @throws Exception 
	 */
	private ResultMap doSend(List<String> mobileList, String message,String smsType) throws Exception{

		ResultMap result = new ResultMap();
		
		log.info("otp code is :"+message);
		
		//开关判断1.如果FLAG为true执行发送，根据具体结果进行判断；2.如果为false,返回成功状态但是不执行发送
		if(smsSwitch(smsType)){
			String s=systemService.getSmsAdContent(smsType);
			message = message + (StringUtils.isBlank(s)?"":s);
			//int resultCode = smsSend.sendSMS((String[])mobileList.toArray(new String[0]), message,"",5);
			Boolean resultBoolean = messageServiceImpl.sendSMSForJincs(mobileList.get(0),message);
			log.info("call send jar get result:" + resultBoolean);
			if(resultBoolean){
				result.setStatus(ResultMap.STATUS.SUCCESS);
				result.setMessage("发送OTP成功");
				return result;
			}else{
				log.error("send otp failed, get status:" +resultBoolean);
				result.setStatus(ResultMap.STATUS.FAILE);
				result.setMessage("发送OTP失败");
				return result;
			}	
		}else{
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setMessage("发送OTP成功");
			return result;
		}	
	}

}
